#coding=utf-8
# !/usr/bin/python
# -*- coding: utf-8 -*-

from com import * 
import struct
from PySide6.QtGui import QPalette, QColor


class Usercfg:
    def __init__(self, ui):
        self.ui = ui
        self.com = ui.com
        self.wl_wirte =[self.ui.lineEdit_217, self.ui.lineEdit_218,
                        self.ui.lineEdit_219, self.ui.lineEdit_220,
                        self.ui.lineEdit_221, self.ui.lineEdit_222,
                        self.ui.lineEdit_223, self.ui.lineEdit_224]
        
        self.wl_read = [self.ui.lineEdit_229, self.ui.lineEdit_230,
                        self.ui.lineEdit_231, self.ui.lineEdit_232,
                        self.ui.lineEdit_233, self.ui.lineEdit_234,
                        self.ui.lineEdit_235, self.ui.lineEdit_236]
        
        self.val = [i for i in range( len(self.wl_wirte) )]
        self.readval = [i for i in range( len(self.wl_wirte) )]

        self.setupui()
        self.UISend_button1 = self.ui.pushButton_21 # 读取按钮
        self.UISend_button2 = self.ui.pushButton_22 # 写入按钮
        self.UISend_button3 = self.ui.pushButton_8 # 载入默认
        self.UISend_button4 = self.ui.pushButton_9 # 载入读取值
        self.UISend_button5 = self.ui.pushButton_18 # 保存默认
        
        self.UISend_button1.clicked.connect(self.ui_pushButton1_cb)
        self.UISend_button2.clicked.connect(self.ui_pushButton2_cb)
        self.UISend_button3.clicked.connect(self.load_default)
        self.UISend_button4.clicked.connect(self.wirte_default)
        self.UISend_button5.clicked.connect(self.load_readval)
        
          
    def setupui(self):
        self.load_default()
        
    def load_ui_cb(self):
        for i in range( len(self.wl_wirte) ):
            self.wl_wirte[i].setText("{:d}".format(self.val[i]))
 
    def load_default(self):
        for i in range( len(self.wl_wirte) ):
            self.val[i] = self.ui.cfg.data['usercfg'][i]
        self.load_ui_cb()
        
    def wirte_default(self):
        for i in range( len(self.wl_wirte) ):
            self.val[i] = str_to_int_safe(self.wl_wirte[i].text())
            self.ui.cfg.data['usercfg'][i] = self.val[i]
        self.ui.cfg.save()
   
    def load_readval(self):   
        for i in range( len(self.wl_wirte) ):
            self.val[i] = self.readval[i]
        self.load_ui_cb()
        
    def ui_pushButton1_cb(self):
        self.ui_cb_send_readmsg()

    def ui_pushButton2_cb(self):
        for i in range( len(self.wl_wirte) ):
            self.val[i] = str_to_int_safe(self.wl_wirte[i].text())
        self.ui_cb_send_writemsg()
        

    def ui_cb_send_readmsg(self):
        if self.com.serdev.ser.isOpen():
            array = self.com.pack_frame_head(0x24, 0x66)
            self.com.pack_frame_tail(array)
            self.com.serdev.ser.write(array)
        else:
            pass  
            
    def ui_cb_send_writemsg(self):
        if self.com.serdev.ser.isOpen():
            array = self.com.pack_frame_head(0x24, 0x36)
            self.pack_wirtemsg(array)
            self.com.pack_frame_tail(array)
            self.com.serdev.ser.write(array)
        else:
            pass
            
    def pack_wirtemsg(self, array):
        array[7] = self.val[0]
        array[8] = self.val[1]
        array[9] = self.val[2] & 0xff
        array[10] = (self.val[2] >> 8)& 0xff    
        array[11] = self.val[3] & 0xff
        array[12] = (self.val[3] >> 8)& 0xff
        array[13] = self.val[4] & 0xff
        array[14] = (self.val[4] >> 8)& 0xff
        array[15] = self.val[5] & 0xff
        array[16] = (self.val[5] >> 8)& 0xff
        array[17] = self.val[6] & 0xff
        array[18] = (self.val[6] >> 8)& 0xff
        array[19] = self.val[7] & 0xff
        array[20] = (self.val[7] >> 8)& 0xff
 
    def decode(self, a):
        self.readval[0] = struct.unpack('<B', a[7:8])[0]
        self.readval[1] = struct.unpack('<B', a[8:9])[0]
        self.readval[2] = struct.unpack('<H', a[9:11])[0]
        self.readval[3] = struct.unpack('<H', a[11:13])[0] 
        self.readval[4] = struct.unpack('<H', a[13:15])[0]
        self.readval[5] = struct.unpack('<H', a[15:17])[0] 
        self.readval[6] = struct.unpack('<H', a[17:19])[0] 
        self.readval[7] = struct.unpack('<H', a[19:21])[0]
        for i in range( len(self.wl_read) ):
            self.wl_read[i].setText("{:d}".format(self.readval[i]))
        

     



           

